package com.adcc.ags.gms.core.util;

import com.adcc.ags.gms.core.entity.GWLog;
import com.adcc.ags.gms.core.entity.GWLogLevel;
import com.adcc.ags.gms.core.mapper.GWLogMapper;
import com.adcc.utility.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 日志工厂类
 */
@Component
@Transactional
public class LogFactory {

    @Autowired
    private GWLogMapper mapper;

    /**
     * DEBUG日志
     * @param log
     */
    public void debug(Class clazz,String log){
        Log.debug(clazz.getName(),log);
    }

    /**
     * DEBUG日志
     * @param clazz
     * @param log
     * @param ex
     */
    public void debug(Class clazz,String log,Exception ex){
        Log.debug(clazz.getName(),log,ex);
    }

    /**
     * INFO日志
     * @param log
     */
    public void info(Class clazz,String log){
        Log.info(clazz.getName(),log);
    }

    /**
     * INFO日志
     * @param log
     */
    public void info(Class clazz,String operator,String log){
        try{
            Log.info(clazz.getName(),log);
            GWLog gwLog = new GWLog(GWLogLevel.INFO.ordinal(), operator, log);
            mapper.saveLog(gwLog);
        }catch (Exception ex){
            Log.error(LogFactory.class.getName(),"info() error",ex);
        }
    }

    /**
     * WORN日志
     * @param log
     */
    public void warn(Class clazz,String log){
        Log.warn(clazz.getName(), log);
    }

    /**
     * WORN日志
     * @param log
     */
    public void warn(Class clazz,String operator,String log){
        try{
            Log.warn(clazz.getName(), log);
            GWLog gwLog = new GWLog(GWLogLevel.WARN.ordinal(), operator, log);
            mapper.saveLog(gwLog);
        }catch (Exception ex){
            Log.error(LogFactory.class.getName(),"warn() error",ex);
        }
    }

    /**
     * WORN日志
     * @param log
     */
    public void warn(Class clazz,String log,Exception e){
        Log.warn(clazz.getName(), log,e);
    }

    /**
     * WORN日志
     * @param log
     */
    public void warn(Class clazz,String operator,String log,Exception e){
        try{
            Log.warn(clazz.getName(), log,e);
            String strLog = log + " Exception:" + e.fillInStackTrace();
            if(strLog.length() >= 4000){
                strLog = strLog.substring(0,3996) + "...";
            }
            GWLog gwLog = new GWLog(GWLogLevel.WARN.ordinal(), operator, strLog);
            mapper.saveLog(gwLog);
        }catch (Exception ex){
            Log.error(LogFactory.class.getName(),"warn() error",ex);
        }
    }

    /**
     * ERROR日志
     * @param clazz
     * @param log
     */
    public void error(Class clazz,String log){
        Log.error(clazz.getName(),log);
    }

    /**
     * ERROR日志
     * @param clazz
     * @param log
     */
    public void error(Class clazz,String operator,GWLogLevel level,String log){
        try{
            Log.error(clazz.getName(),log);
            GWLog gwLog = new GWLog(level.ordinal(), operator, log);
            mapper.saveLog(gwLog);
        }catch (Exception ex){
            Log.error(LogFactory.class.getName(),"error() error",ex);
        }
    }

    /**
     * ERROR日志
     * @param clazz
     * @param log
     */
    public void error(Class clazz,String log,Exception e){
        Log.error(clazz.getName(),log,e);
    }

    /**
     * ERROR日志
     * @param clazz
     * @param operator
     * @param level
     * @param log
     * @param e
     */
    public void error(Class clazz,String operator,GWLogLevel level,String log,Exception e){
        try{
            Log.error(clazz.getName(),log,e);
            String strLog = log + " Exception:" + e.fillInStackTrace();
            if(strLog.length() >= 4000){
                strLog = strLog.substring(0,3996) + "...";
            }
            GWLog gwLog = new GWLog(level.ordinal(), operator, strLog);
            mapper.saveLog(gwLog);
        }catch (Exception ex){
            Log.error(LogFactory.class.getName(),"error() error",ex);
        }
    }
}
